Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
how to parse "" to long("0") in dotnet

given the following code:

string example = "1234";
long parsed_example = long.Parse(example);
Console.Writeline(parsed_example);
# => 1234

Works great.

the following example does not:

string example = "";
long parsed_example = long.Parse(example);
# [System.FormatException: Input string was not in a correct format.]

However the goal is:

string example = "";
if (example == "")
{
    example = "0";
}
long parsed_example = long.Parse(example);
Console.Writeline(parsed_example);
# => 0

is there a shorter, apropriate solution? The above code would almost justify a tiny function, Id preferable have a inline solution. Maybe something such as (pseudo code):

string example = "";
long parsed_example = example ?? 0, long.Parse(example);
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

long parsed_example = example == "" ? 0 : long.Parse(example);

However: don't be obsessed with single-line solutions; a multi-line solution is often more readable and correct. There are no prizes for creating complex code. You may also wish to look at string.IsNullOrWhiteSpace, long.TryParse, etc. For example:

long value;
if (string.IsNullOrWhiteSpace(example))
{
    // what you want to do with blank/empty values
    value = 42;
}
else if (!long.TryParse(example, out value))
{
    // what you want to do with non-integer values 
   value = 84;
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda